﻿using System.Collections.Generic;
using FairyGUI;
using UnityEngine;

namespace GameMain.Scripts.UI.Framework
{
    // UI的最大单位 直接挂在FairyGUI组件的身上
    public abstract class UIPage : MonoBehaviour
    {
        protected UIPanel _uiPanel;
        public GComponent View { get; set; }

        protected virtual int sortingOrder { get; } = 1;
        
        protected List<IUIUpdate> _updateUI = new List<IUIUpdate>();

        protected virtual void Awake()
        {
            _uiPanel = GetComponent<UIPanel>();
            if (_uiPanel != null)
            {
                View = _uiPanel.ui;
                _uiPanel.SetSortingOrder(sortingOrder, true);
            }
            else
            {
                DisplayObjectInfo info = gameObject.GetComponent<DisplayObjectInfo>();
                View = GRoot.inst.DisplayObjectToGObject(info.displayObject).asCom;
                View.sortingOrder = sortingOrder;
            }
            AutoAdapt();
            View.fairyBatching = true;
            View.visible = false;
            UIMgr.AddPage(this);
            
            void AutoAdapt()
            {
                var rootW = GRoot.inst.width;
                var rootH = GRoot.inst.height;
                //设置Page大小
                View.SetSize(rootW,rootH);
                //自适应背景
                var adaptBg = View.GetChild("adaptBg");
                if(adaptBg == null) return;
                var scale = UIUtils.GetFullScreenScale(adaptBg.width, adaptBg.height);
                adaptBg.SetSize(adaptBg.width * scale,adaptBg.height * scale);
                adaptBg.Center();
            }
        }

        public void Open()
        {
            OnOpen();
            View.visible = true;
        }
        protected virtual void OnOpen() { }

        protected virtual void Update()
        {
            for (var i = 0; i < _updateUI.Count; i++)
            {
                _updateUI[i].RenderUpdate();
            }
#if UNITY_EDITOR
        if(dispose)
            Dispose();
#endif
        }
#if UNITY_EDITOR
        public bool dispose; //测试GC用的
#endif
        
        public void AddChild(UIBase child)
        {
            //暂时不保存子类
            if (child is IUIUpdate update)
            {
                RemoveUpdate(update);
                AddUpdate(update);
            }
        }
        public void AddUpdate(IUIUpdate update) => _updateUI.Add(update);

        public void RemoveChild(UIBase child, bool isDispose = false)
        {
            if (child is IUIUpdate update)
            {
                if(!(child is IUIUpdateHide) || isDispose)
                    RemoveUpdate(update);
            }
        }
        public void RemoveUpdate(IUIUpdate update) => _updateUI.Remove(update);

        public void Close()
        {
            View.visible = false;
            OnClose();
        }
        protected virtual void OnClose() { }
        
        public void Dispose()
        {
            OnDispose();
            UIMgr.RemovePage(this);
            if(_uiPanel != null)
                Destroy(gameObject);
            else
                View.Dispose();
        }
        protected virtual void OnDispose() { }
    }
}

